<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10 0010
 * Time: 上午 10:01
 */
namespace app\admin\controller;
use \think\Db;
class Content extends Admin{

    //当前模块基础信息
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '网站信息管理',
                'description' => '管理网站内的新闻、公告和活动',
            ),
            'menu' => array(
                array(
                    'name' => '站内信息',
                    'url' => url('contentList')
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加内容',
                    'url' => url('contentAdd'),
                ),
            ),
        );
    }

    //内容列表
    public function contentList(){
        $list = Db::name('contents')->order('id desc')->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //内容添加、修改
    public function contentAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            if($data['recommend_position'] == 1){
                if($data['recommend'] == 2){
                    return ajaxReturn(500,'请先设置推荐');
                }
                if(Db::name('contents')->where(['type'=>1,'recommend_position'=>1])->count() >= 4){
                    return ajaxReturn(500,'图集只能推荐四个');
                }
            }
            if($data['id']){
                $content = '内容修改';$status = 2;
                Db::name('contents')->where('id',$data['id'])->update($data);
            }else{
                $content = '内容添加';$status = 1;
                $data['create_time'] = time();
                Db::name('contents')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('contentList'));
        }
//        dump(Db::name('contents')->where('id',input('id'))->find());die;
        $this->view->info = Db::name('contents')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //内容删除
    public function del(){
        $content = '内容删除';$status = 3;
        if($image = Db::name('contents')->where('id',input('id'))->value('image')){
            if(!unlink(ROOT_PATH.'/public'.$image)) {
                return ajaxReturn('500','内置错误，请尽快联系管理员！');
            }
        }
        $this->log($content,$status);
        Db::name('contents')->delete(input('id'));
        return ajaxReturn('200');
    }
}